import { createStore } from 'vuex'

export default createStore({
  state: {
    cartList: [], // 购物车列表数据
    orderList: [], // 生成订单列表数据
    edit: true, //编辑的状态

    userAddress: [
      {
        id: 1001,
        name: '鲜雨涵',
        tel: '13123456731',
        province: '广东省',
        city: '广州市',
        county: '天河区',
        addressDetail: 'xxxxx',
        isDefault: true,
        areaCode: '110101',
      },
      {
        id: 1002,
        name: '鲜涛',
        tel: '13123456001',
        province: '广东省',
        city: '广州市',
        county: '天河区',
        addressDetail: 'asdfghjkl',
        isDefault: false,
        areaCode: '110101',
      },
      {
        id: 1003,
        name: '陈锔',
        tel: '13123456701',
        province: '广东省',
        city: '广州市',
        county: '天河区',
        addressDetail: '哈哈哈哈哈',
        isDefault: false,
        areaCode: '110101',
      },
    ],
    orderListed: [], // 生成订单后的列表数据
  },
  mutations: {
    ADDCART(state, value) {
      state.cartList = value
    },

    PAY(state, value) {
      state.orderList = value
    },

    DELETE(state, value) {
      state.cartList = value
    },

    EDIT(state, value) {
      if (value === 'delete') {
        state.edit = true
      } else {
        state.edit = !state.edit
      }
    },

    // 生成订单后

    UPDATEORDER(state) {
      state.orderListed = state.orderListed.concat(state.orderList)
    },

    // 新增地址

    ADDADDRESS(state, value) {
      state.userAddress.map(it => {
        if (value.isDefault) {
          it.isDefault = false
        }
      })

      state.userAddress.push(value)
    },

    // 编辑地址-保存地址
    CHANGEADDRESS(state, value) {
      state.userAddress = state.userAddress.map(it => {
        if (value.isDefault) {
          it.isDefault = false
        }

        return it.id === value.id ? value : it
      })
    },

    // 地址删除

    DELETEADDRESS(state, value) {
      state.userAddress = state.userAddress.filter(it => {
        return !(value.id === it.id)
      })

      if (value.isDefault && state.userAddress.length) {
        state.userAddress[0].isDefault = true
      }
    },
  },
  actions: {},
})
